package com.handmark.pulltorefresh.library;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;


/**
 * RotateLoading Created by Victor on 2015/4/28.
 */
public class YYKLoadingView extends View {

	private static final int DEFAULT_WIDTH = 6;
	private static final int DEFAULT_SHADOW_POSITION = 5;

	private Paint mPaint;

	private RectF loadingRectF;
	private RectF shadowRectF;

	private int topDegree = 10;
	private int bottomDegree = 190;

	private float arc;

	private int width;

	private boolean changeBigger = true;

	private int shadowPosition;

	private boolean isStart = false;

	private Rect loadRect;

	private int color;
	private Bitmap mLoadImage;

	public YYKLoadingView(Context context) {
		super(context);
		initView(context, null);
	}

	public YYKLoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs);
	}

	public YYKLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context, attrs);
	}

	private void initView(Context context, AttributeSet attrs) {
		color = Color.WHITE;
		width = dpToPx(context, DEFAULT_WIDTH);
		shadowPosition = dpToPx(getContext(), DEFAULT_SHADOW_POSITION);

		if (null != attrs) {
			TypedArray typedArray = context.obtainStyledAttributes(attrs,
					R.styleable.RotateLoading);
			color = typedArray.getColor(R.styleable.RotateLoading_loading_color,
					Color.WHITE);
			width = typedArray.getDimensionPixelSize(
					R.styleable.RotateLoading_loading_width,
					dpToPx(context, DEFAULT_WIDTH));
			shadowPosition = typedArray.getInt(R.styleable.RotateLoading_shadow_position,
					DEFAULT_SHADOW_POSITION);
			typedArray.recycle();
		}

		mPaint = new Paint();
		mPaint.setColor(color);
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(width);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mLoadImage = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon_loading);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		arc = 10;
		loadingRectF = new RectF(2 * width, 2 * width, w - 2 * width, h - 2 * width);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (!isStart) {
			return;
		}

		mPaint.setColor(getResources().getColor(R.color.colorPrimary));
		canvas.drawArc(loadingRectF, topDegree, arc, false, mPaint);
		canvas.drawBitmap(mLoadImage, null, loadingRectF, mPaint);

		topDegree += 10;
		bottomDegree += 10;
		if (topDegree > 360) {
			topDegree = topDegree - 360;
		}
		if (bottomDegree > 360) {
			bottomDegree = bottomDegree - 360;
		}

		if (changeBigger) {
			if (arc < 160) {
				arc += 2.5;
				invalidate();
			}
		} else {
			if (arc > 10) {
				arc -= 5;
				invalidate();
			}
		}
		if (arc == 160 || arc == 10) {
			changeBigger = !changeBigger;
			invalidate();
		}
	}

	public void start() {
		startAnimator();
		isStart = true;
		invalidate();
	}

	public void stop() {
		stopAnimator();
		invalidate();
	}

	public boolean isStart() {
		return isStart;
	}

	private void startAnimator() {
		ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 0.0f, 1);
		ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 0.0f, 1);
		scaleXAnimator.setDuration(300);
		scaleXAnimator.setInterpolator(new LinearInterpolator());
		scaleYAnimator.setDuration(300);
		scaleYAnimator.setInterpolator(new LinearInterpolator());
		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
		animatorSet.start();
	}

	private void stopAnimator() {
		ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1, 0);
		ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 1, 0);
		scaleXAnimator.setDuration(300);
		scaleXAnimator.setInterpolator(new LinearInterpolator());
		scaleYAnimator.setDuration(300);
		scaleYAnimator.setInterpolator(new LinearInterpolator());
		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
		animatorSet.addListener(new Animator.AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {
				isStart = false;
			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}
		});
		animatorSet.start();
	}

	public int dpToPx(Context context, float dpVal) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
				context.getResources().getDisplayMetrics());
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		start();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		stop();
	}
}
